'use strict'

const path = require('path');
const { TaiSpider } = require('tai-spider');
const { saveJsonFile } = require('../utils/file-helper');
const { formatDate } = require('../utils/str-helper');

class SpaceflightnowMissionSpider extends TaiSpider {

    constructor(options = {}) {
        super(options);
        this.name = 'spaceflightnow-mission';
        this.debug = true;
        this.start_urls = [
            'https://spaceflightnow.com/category/mission-reports/',
            // 'https://spaceflightnow.com/2019/04/29/hayabusa-2-locates-artificial-crater-on-asteroid/',
        ];
    }

    *parse(response) {
        yield* response.follow_all(response.css('.nav-links a'))

        for (let ele of response.css('article')) {
            yield response.follow(ele.css('.entry-title a'), this.parseArticle);
        }
    }

   *parseArticle(response, spider) {
        // const { store } = spider.options;
        let data = response.extract();
        let dateStr = response.css('.entry-header .entry-meta-date').extract_first();
        let date = formatDate(dateStr)
        data.timestamp = date.valueOf();
        data.date = date.format();
        // yield saveJsonFile(path.join(store.path, data.title + '.json'), data)

        yield Object.assign({
            index_name: 'tai-article',
        }, data);
    }

}

module.exports = SpaceflightnowMissionSpider;